package com.hefeng.unit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefeng.common.core.domain.MapEntity;
import com.hefeng.common.core.utils.AmapUtil;
import com.hefeng.common.core.utils.EmptyUtils;
import com.hefeng.unit.common.StartLoadingParameter;
import com.hefeng.unit.domain.DeviceInfo;
import com.hefeng.unit.domain.GyDevice;
import com.hefeng.unit.domain.GyDeviceSon;
import com.hefeng.unit.mapper.SonMapper;
import com.hefeng.unit.service.SonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SonServiceImpl extends ServiceImpl<SonMapper, GyDeviceSon> implements SonService {
    @Autowired
    private SonMapper sonMapper;
    @Autowired
    private RestTemplate restTemplate;


    /**
     * 获取登录用户子设备列表
     * @param userId
     * @param current
     * @param size
     * @return
     */
    @Override
    public Page<GyDeviceSon> getSonList(String userId, long current, long size) {
        Page<GyDeviceSon> pagin = new Page<>(current,size,true);
        if(EmptyUtils.isEmpty(userId)){
            return null;
        }
        IPage<GyDeviceSon> gyDevicePage =  sonMapper.selectDeviceList(userId,pagin);

        return (Page<GyDeviceSon>) gyDevicePage;
    }

    @Override
    public GyDeviceSon select(GyDeviceSon  gyDeviceSon) {
        LambdaQueryWrapper<GyDeviceSon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GyDeviceSon::getId,gyDeviceSon.getId());
        return sonMapper.selectOne(queryWrapper);
    }

    @Override
    public List<GyDeviceSon> list(String deviceCode) {
        LambdaQueryWrapper<GyDeviceSon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GyDeviceSon::getDeviceCode,deviceCode);
        queryWrapper.eq(GyDeviceSon::getDeviceType,1);
//        sonMapper.selectList(queryWrapper);
        return sonMapper.selectList(queryWrapper);
    }

    @Override
    public int update(GyDeviceSon gyDeviceSon) {
        return sonMapper.updateById(gyDeviceSon);
    }

    @Override
    public Map<String, Object> updateZ(GyDeviceSon gyDeviceSon) {
        Map<String, Object> dataMap = new HashMap<>();

        try {
            //1. 根据条件动态更新
            LambdaQueryWrapper<GyDeviceSon> queryWrapper = new LambdaQueryWrapper<>();

            queryWrapper.eq(GyDeviceSon::getId,gyDeviceSon.getId());
            //获取设备城市编码
//            MapEntity gas = AmapUtil.getTowncodeByLocation(gyDeviceSon.getDeviceLongitude(), gyDeviceSon.getDeviceLatitude());
//            gyDeviceSon.setCitycode(gas.getCitycode());
//            gyDeviceSon.setAdcode(gas.getAdcode());
//            gyDeviceSon.setTowncode(gas.getTowncode());
            sonMapper.update(gyDeviceSon, queryWrapper);

            GyDeviceSon deviceson = sonMapper.selectOne(queryWrapper);

            DeviceInfo gasDeviceInfo = new DeviceInfo();
            //设备编码
            gasDeviceInfo.setDeviceCode(gyDeviceSon.getDeviceCode());
//            //经度
//            gasDeviceInfo.setLongitude(Double.parseDouble(gyDeviceSon.getDeviceLongitude()));
//            //经度
//            gasDeviceInfo.setLatitude(Double.parseDouble(gyDeviceSon.getDeviceLatitude()));
            //0.工业端 1.居民端
            gasDeviceInfo.setDeviceInstallationType("0");
            //设备名称
            gasDeviceInfo.setDeviceName(gyDeviceSon.getDeviceName());
            //设备安装地址
            gasDeviceInfo.setDeviceAddress(gyDeviceSon.getDeviceLocation());
            restTemplate.put(StartLoadingParameter.DEVICEUPDATEINFO, gasDeviceInfo);

            dataMap.put("make", true);

            dataMap.put("device", deviceson);

        } catch (Exception e) {

            dataMap.put("msg", "修改失败！");

        }
        return dataMap;
    }
}
